// pagesEnterprise/pages/houseOrder/houseOrder.js
import ajax from '../../../utils/index';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gardenId: '',
    records: [],
    pageNum: 1,
    reachbottom: false,
    parameter: ''
  },
  searchChange(event) {
    this.data.parameter = event.detail.value
  },
  searchConfirm() {
    this.data.pageNum = 1
    this.get_park_work_records()
  },
  /**
     * 接口 - 看房工单列表
     */
  get_park_work_records: function () {
    let that = this
    ajax.get('/wx/appletWorkOrder/getListByGardenId', {
      parameter: this.data.parameter,
      gardenId: that.data.gardenId,
      pageSize: 10,
      pageNum: that.data.pageNum
    }).then(res => {
      if (res.code == 200) {
        let rows = res.rows.map(ele => {
          ele.createTime = ele.createTime ? ele.createTime.split(' ')[0] : ''
          ele.show = true
          return ele
        })
        console.log('列表', rows)
        that.setData({
          records: that.data.pageNum == 1 ? rows : [...that.data.records, ...rows]
        })
        if (res.rows.length >= 10) {
          that.setData({
            reachbottom: true
          })
        } else {
          that.setData({
            reachbottom: false
          })
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 打开详情
   */
  showDetail: function (e) {
    let index = e.currentTarget.id
    let records = this.data.records
    records[index].show = !records[index].show
    this.setData({
      records
    })
    console.log(records)
  },

  /**
   * 打电话
   */
  callPhone: function (e) {
    if (!e.currentTarget.dataset.tel) return ajax.showToast("没有电话号码")
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel,
    })
    // this.setData({
    //     records: []
    // })
    let that = this
    ajax.get('/wx/appletWorkOrder/editInfo', {
      id: parseInt(e.currentTarget.id)
    }).then(res => {
      // if (res.code == 200) {
      //     that.get_park_work_records()
      // }
    })
  },

  /**
   * 微信支付
   */
  wxPay: function (e) {
    let item = e.currentTarget.dataset.item
    ajax.get('/wx/appletWorkOrder/pay', {
      orderNo: item.orderNo,
      userId: item.userId
    }).then(res => {
      if (res.appId && res.nonceStr) {
        wx.requestPayment({
          timeStamp: res.timeStamp,
          nonceStr: res.nonceStr,
          package: res.packAge,
          signType: 'MD5',
          paySign: res.paySign,
          success(re) {
            console.log(re)
            if (re.errMsg == 'requestPayment:ok') {
              wx.showToast({
                title: '支付成功',
              })
            }
          },
          fail(re) {
            console.log(re)
            if (re.errMsg == 'requestPayment:fail cancel') {
              wx.showToast({
                title: '您已取消',
                icon: 'error'
              })
            } else {
              wx.showToast({
                title: re.errMsg,
                icon: 'none'
              })
            }
          }
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      gardenId: options.id
    })
    this.get_park_work_records()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.reachbottom) {
      let pageNum = this.data.pageNum
      pageNum++
      this.setData({
        pageNum
      })
      this.get_park_work_records()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})